package org.csu.linkgame.controller;

import de.felixroske.jfxsupport.FXMLController;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.SortedList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import org.csu.linkgame.domain.GameHistory;
import org.csu.linkgame.domain.User;

import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Comparator;
import java.util.ResourceBundle;

@FXMLController
public class HistoryViewController implements Initializable {
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:3306/super_link?useUnicode=true&characterEncoding=utf8";


    // 数据库的用户名与密码，需要根据自己的设置
    static final String USER = "root";
    static final String PASS = "2003052288mjp";
    @FXML
    public TableView myHistory;
    @FXML
    public TableColumn myTime;
    @FXML
    public TableColumn myWinner;
    @FXML
    public TableColumn myLoser;
    @FXML
    public Button searchButton;
    @FXML
    public TextField input;
    @FXML
    final ObservableList<GameHistory> data = FXCollections.observableArrayList();
    User user;
    @Override
    public void initialize(URL location, ResourceBundle resources) {
    }
    public void setUser(User user) {
        this.user = user;
    }
    public void initializeData() throws Exception{
        // 进行数据绑定
        myTime.setCellValueFactory(
                new PropertyValueFactory<GameHistory,String>("endTime")
        );
        myWinner.setCellValueFactory(
                new PropertyValueFactory<GameHistory,String>("winner")
        );
        myLoser.setCellValueFactory(
                new PropertyValueFactory<GameHistory,String>("loser")
        );
        // 连接数据库
        Connection conn = null;
        Statement stmt = null;
        Class.forName(JDBC_DRIVER);
        conn = DriverManager.getConnection(DB_URL,USER,PASS);

        stmt = conn.createStatement();
        String sql;
        //sql = "select username,user_goal,user_rank from user";
        //sql = "select * from user order by user_rank";
        sql = "select * from game_history where winner = \""+user.getUsername()+"\" or loser = \""+user.getUsername()+"\" order by end_time";
        ResultSet rs = stmt.executeQuery(sql);
        while(rs.next()){
            data.add(new GameHistory( rs.getInt(1),rs.getDate(2),rs.getString(3)
                    ,rs.getString(4),rs.getString(5)));
        }
        myHistory.setItems(data);
        conn.close();
        stmt.close();

    }
    public void onSearchButtonClicked(MouseEvent mouseEvent) throws Exception {
        //this.sortedList.clear();
        data.clear();
        String username = input.getText();
        // 连接数据库
        Connection conn = null;
        Statement stmt = null;
        Class.forName(JDBC_DRIVER);
        conn = DriverManager.getConnection(DB_URL,USER,PASS);

        stmt = conn.createStatement();
        String sql;
        //sql = "select username,user_goal,user_rank from user";
        //sql = "select * from user order by user_rank";
        if(username.equals("")){
            sql = "select * from game_history where winner = \""+user.getUsername()+"\" or loser = \""+user.getUsername()+"\" order by end_time";
        }else{
            sql = "select * from game_history where winner = \""+username+"\" or loser = \""+username+"\" order by end_time";
        }
        ResultSet rs = stmt.executeQuery(sql);
        while(rs.next()){
            data.add(new GameHistory( rs.getInt(1),rs.getDate(2),rs.getString(3)
                    ,rs.getString(4),rs.getString(5)));
        }
        myHistory.setItems(data);
        conn.close();
        stmt.close();
    }
}
